package com.cloudcc.mobile.view.web;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cloudcc.cloudframe.util.ListUtils;
import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.ReceiverConstant;
import com.cloudcc.mobile.view.base.BaseWebViewFragment;

public class SyncWebFragment extends BaseWebViewFragment {

	protected static final int STEP_START = 1;
	protected static final int STEP_OVERRIDEURL = 2;
	protected static final int STEP_FINISH = 3;

	private List<MonitorParam> mSyncParamArray = new ArrayList<MonitorParam>();

	private MonitorParam currentMonitorParam;// 当前正在检测的 监控器
	@Override
	public WebViewClient getWebViewClient() {
		return new SyncWebClient();
	}
	private class SyncWebClient extends BaseWebViewClient {
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			if(hasEnterMonitor(url, STEP_START)){
				onEnterMonitor(view, url);
			}
			if (hasEnterTrigger(url, STEP_START)) {
				onTriggerMonitor(view, url);
			}
			super.onPageStarted(view, url, favicon);
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			if(hasEnterMonitor(url, STEP_FINISH)){
				onEnterMonitor(view, url);
			}
			if (hasEnterTrigger(url, STEP_FINISH)) {
				onEnterMonitor(view, url);
			}
			super.onPageFinished(view, url);
		}
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			LogUtils.d("webview", "跳转url:" + url);
			if(hasEnterMonitor(url, STEP_OVERRIDEURL)){
				onEnterMonitor(view, url);
			}
			if (hasEnterTrigger(url, STEP_OVERRIDEURL)) {
				onEnterMonitor(view, url);
				return true;
			}
			return false;
		}
	}
	
	/**
	 * @param url
	 * @param step
	 * @return
	 */
	private boolean hasEnterMonitor(String url, int step) {
		if (!hasParamArray()) {
			return false;
		}
		boolean isEnterMonitor = false;// 是否进入监控
		for (MonitorParam monitor : mSyncParamArray) {
			// 监控参数为空 监控 进入key 为空 监控 开始步骤 跟 开始不同
			if (monitor == null || monitor.enterMonitorKey == null
					|| monitor.enterMonitorStep != step) {
				continue;
			}
			// 监控 开始
			for (String key : monitor.enterMonitorKey) {
				if (StringUtils.contains(url, key)) {
					currentMonitorParam = monitor;
					isEnterMonitor = true;
					break;
				}
			}

		}
		return isEnterMonitor;
	}

	/**
	 * 是否进入了 触发key
	 * 
	 * @param url
	 * @return
	 */
	private boolean hasEnterTrigger(String url, int step) {
		if (!hasParamArray() || !hasMonitor()) {// 先判断是否已经进入 监控
			return false;
		}

		boolean isTrigger = false;
		for (MonitorParam monitor : mSyncParamArray) {

			if (monitor == null || monitor.triggerMonitorKey == null
					|| monitor.triggerMonitorStep != step) {
				continue;
			}
			for (String key : monitor.triggerMonitorKey) {
				if (StringUtils.contains(url, key)) {
					isTrigger = true;
					break;
				}
			}
		}
		return isTrigger;
	}

	/**
	 * 是否存在 监控参数
	 * 
	 * @return
	 */
	private boolean hasParamArray() {
		return mSyncParamArray != null && ListUtils.isEmpty(mSyncParamArray);
	}

	/**
	 * 检测是否已经进入 监控
	 * 
	 * @return
	 */
	private boolean hasMonitor() {
		return currentMonitorParam != null;
	}

	public static class MonitorParam {
		public String[] enterMonitorKey;// 开始进入连接
		public int enterMonitorStep;// 进入监控的步骤
		public String[] triggerMonitorKey;// 触发的关键
		public boolean close;// 是否关闭当前页面
		public int triggerMonitorStep;
		public MonitorParam(String[] enterMonitorKey, int enterMonitorStep,
				String[] triggerMonitorKey, boolean close,
				int triggerMonitorStep) {
			super();
			this.enterMonitorKey = enterMonitorKey;
			this.enterMonitorStep = enterMonitorStep;
			this.triggerMonitorKey = triggerMonitorKey;
			this.close = close;
			this.triggerMonitorStep = triggerMonitorStep;
		}

	}

	/**
	 * 新增监控器
	 * 
	 * @param monitor
	 * @return
	 */
	public void addMonitorParam(MonitorParam monitor) {
		mSyncParamArray.add(monitor);
	}

	public void onEnterMonitor(WebView view, String url) {
		
	}
	private void sendBoardReceiver(WebView view, String url){
		Intent intent=new Intent(ReceiverConstant.RECEIVE_SYNC_WEB);
		view.getContext().sendBroadcast(intent);
		view.stopLoading();
		if (currentMonitorParam.close) {
			try {
				((Activity)view.getContext()).finish();
			} catch (Exception e) {
			}
		}
	}

	public void onTriggerMonitor(WebView view, String url) {
		sendBoardReceiver(view, url);
	}
	
	public static  WebSyncDefaultProxy creatDefaultProxy(){
		return new WebSyncDefaultProxy("");
	}
	
}
